function [] = combineCovariates(geoLevel, timeLevel)

data1File        = sprintf('../data/data_mat/covariates/data1_%s_%s.mat', geoLevel, timeLevel);
data2File        = sprintf('../data/data_mat/covariates/data2_%s.mat', geoLevel);
data3File        = sprintf('../data/data_mat/covariates/data3_%s.mat', timeLevel);
periodLabelsFile = sprintf('../data/data_csv/covariates/periodLabels_%s.csv', timeLevel);
outputFile       = sprintf('../data/data_mat/covariates/covariates_%s_%s.mat', geoLevel, timeLevel);

% Load input files
load(data1File);
load(data2File);
load(data3File);

periodLabels = readtable(periodLabelsFile);
timeIds    = table2array(periodLabels(:,1)); % T x 1
timeLabels = table2cell(periodLabels(:,2)); % T x 1
if strcmp(timeLevel, 'day')
	timeLabels = cellfun(@(x) datestr(x, 'yyyy-mm-dd') , timeLabels,'UniformOutput',false);
end

% Get covariates
X      = timeLocationData.values; % T x K x NumX
Z      = locationData.values;     % K x NumZ
A      = timeData.values;         % T x NumA

% Get names of the covariates
Xnames      = timeLocationData.VarNames{2};
Znames      = locationData.VarNames{2};
Anames      = timeData.VarNames{2};

% Get labels of geographic entities and of periods
geoLabels	=  timeLocationData.labels{1}; % 1 x K

% Check that the dimensions are consistent between timeLocationData, locationData and timeData
if size(X,1) ~= size(A,1) || size(X,2) ~= size(Z,1)
	error('Inconsistent dimensions of X with A or Z.');
end
if size(X,1) ~= length(timeLabels) || size(X,2) ~= length(geoLabels)
	error('Inconsistent dimensions of X with timeLabels or geoLabels.');
end
if size(X,3) ~= length(Xnames) || size(Z,2) ~= length(Znames) || size(A,2) ~= length(Anames)
	error('Inconsistent dimensions of X with Xnames or Z with Znames or A with Anames.');
end

% Check that the labels are consistent
geoLabels2 = locationData.labels{1};
timeIds2 = timeLocationData.labels{2};  % 1 x T
timeIds3 = timeData.labels{1};
if ~isequal(geoLabels, geoLabels2)
    error('Inconsistent geoLabels!');
end
if ~isequal(timeIds, timeIds2) || ~isequal(timeIds, timeIds3)
    error('Inconsistent timeIds!');
end

% Store everything into object data
data.X = X;
data.Z = Z;
data.A = A;
data.Xnames = Xnames;
data.Znames = Znames;
data.Anames = Anames;
data.geoLabels = geoLabels;
data.timeLabels = timeLabels;

% Save to output file
save(outputFile, 'data', '-v7.3');
end
